<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/8/9
 * Time: 10:45
 */

namespace Biz\service;

use Biz\cache\RecommendByKeyWord as cRecommendByKeyWord;
//use Biz\service\search\Goods as searchGoods;
use Biz\service\Goods as sGoods;
use Biz\model\GoodsBasicInfo as mGoodsBasicInfo;
use function Composer\Autoload\includeFile;
use Biz\service\esSearch\Goods as esGoods;
/**
 * @deprecated
 */
class MallGoodsList
{
    /*
     * 根据标签获取商品列表
     * **/
    public static function RecommendByTag($tag, $page, $pageSize, $sortType, $areaCode = 0, $params = [])
    {
        $result = SearchGoods::searchGoodsByTag($tag, $sortType, $page, $pageSize, $params);
        // todo 搜索切换es待删除
//        $results = searchGoods::searchGoodsByTag($tag, $sortType, $page, $pageSize , $params);
        $list = self::formatRecommendGoods($result['rows'], $areaCode);
        $list['is_over'] = $result['is_over'];
        return $list ?: [];
    }

    /*
     * 根据关键词获取商品列表RecommendByTag
     * **/
    public static function RecommendByKeyWord($keyWord, $page, $pageSize, $areaCode)
    {
        $keyWord = self::getKeyword($keyWord);
        $goodsIds = cRecommendByKeyWord::getGoods($keyWord);
        $start = ($page - 1) * $pageSize;
        $goodsIds = array_slice($goodsIds, $start, $pageSize);
        $count = count($goodsIds);
        $where = [
            'id' => $goodsIds,
            'source' => esGoods::SOURCE_APP
        ];
        $result = SearchGoods::searchEntrance($where, esGoods::SCENES_GOODS_FILTER);
        $list = self::formatRecommendGoods($result['rows'], $areaCode);
        $list['is_over'] = $count < $pageSize ? 1 : 0;
        return $list;
    }

    public static function RecommendByKeyWordFormBanner($keyWord, $pageSize, $areaCode)
    {
        $keyWord = self::getKeyword($keyWord);
        $goodsIds = cRecommendByKeyWord::getGoods($keyWord);

        $goodsIds = array_slice($goodsIds, 0, 20);
        $list = self::formatRecommendGoods($goodsIds, $areaCode);
        $list['list'] = array_slice($list['list'], 0, $pageSize);
        return $list ?: [];
    }

    /*
     * 处理数据
     * **/
    private static function formatRecommendGoods($goodsIds, $areaCode)
    {
        $goodslist = sGoods::goods($goodsIds, sGoods::FIELDS_GROUP_LIST, ['area_code' => $areaCode]);

        foreach($goodslist as $info)
        {
            if($info['status'] == mGoodsBasicInfo::STATUS_APP_NOMAL) {
                $list[] = $info;
            }
        }

        $is_over = 0;
        if (count($goodsIds) != 20) {
            $is_over = 1;
        }

        $list = array(
            'list' => ($goodslist && is_array($goodslist)) ? array_values($goodslist) : array(),
            'is_over' => $is_over,
        );
        return $list;
    }

    /*
     * 处理关键字
     * **/
    private static function getKeyword($keyWord)
    {
        switch ($keyWord) {
            case '热门':
                $keyWord = 'hot';
                break;
            case '上新':
                $keyWord = 'new';
                break;
        }

        return $keyWord;
    }
}